package com.client.web;

import com.client.dao.UserDao;
import com.client.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * UserController
 *
 * @author humdeef
 * @date 2018/11/12
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserDao userDao;

    @RequestMapping("/save")
    public String save(@RequestParam Long id, @RequestParam String name, @RequestParam Integer age, @RequestParam Integer sex) {
        User o = new User();
        o.setId(id);
        o.setName(name);
        o.setAge(age);
        o.setSex(sex);
        int save = userDao.save(o);
        return "Ok";
    }

    @RequestMapping("/update")
    public String update(@RequestParam(required = true) Long id,
                         @RequestParam(required = false) String name,
                         @RequestParam(required = false) Integer age,
                         @RequestParam(required = false) Integer sex) {
        User o = new User();
        o.setId(id);
        o.setName(name);
        o.setAge(age);
        o.setSex(sex);
        userDao.update(o);
        return "Ok";
    }

    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable("id") Long id) {
        User o = new User();
        o.setId(id);
        userDao.delete(o);
        return "Ok";
    }
    @RequestMapping("/select")
    @ResponseBody
    public List<User> selectAll() {
        User o = new User();
       return userDao.selectAll(o);
    }

}
